// pages/offerDetails/offerDetails.js
var api = require('../../utils/api.js');
var app = getApp();
var WxParse = require('../../wxParse/wxParse.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
      indicatorDots: false,
      autoplay: false,
      interval: 3000,
      duration: 500,
      swiperIndex:1,
      swipertotalNum:0,
      moreState:true,
      configurationState:true,
      actionSheetHidden: true,
      hiddenmodalput: true, //隐藏模态框
      sexState:0,  //性别状态

      car_id : 1, //车型ID
      ver_id:0,   //版本ID,
      detail:{},  //车详情

      company: {},  //公司资料

      //预约到店
      orderTel:'',
      orderName:'',
      photos:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var car_id = options.car_id;
    var ver_id = options.ver_id;

    console.log(options)

    if (ver_id == undefined || ver_id == ''){
      ver_id = 0;
    }

    that.setData({
      car_id: car_id,
      ver_id: ver_id
    })

   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('aaa_show')

    var that = this;
    var car_id = that.data.car_id;
    var ver_id = that.data.ver_id;

    //车详情
    try {
      var detail = wx.getStorageSync('car_detail_' + car_id + '_v_' + ver_id)
      if (detail) {
        WxParse.wxParse('post_content', 'html', detail.post_content, this, 30);

        var swipertotalNum = detail.more.photos.length             //获取轮播的总数
        that.setData({
          detail: detail,
          swipertotalNum: swipertotalNum
        })
      } else {
        that.getCarDetail();
      }
    } catch (e) {
      that.getCarDetail();
    }

    //公司资料
    try {
      var company = wx.getStorageSync('company')
      if (company) {
        that.setData({
          company: company
        });
      }
    } catch (e) {
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
   
    this.getCarDetail();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  onSlideChangeEnd: function (e) {
      var that = this;

      that.setData({
          swiperIndex: e.detail.current + 1            //设置当前轮播页数
      })
  },
  configurationEvent:function(e){
    console.log(e)
    var form_id = e.detail.formId;
    //保存form_id
    app.cacheFormIDs(form_id);
    
      var that = this;
      that.setData({
          configurationState: (!that.data.configurationState)   //打开更多配置
      })
  },
  //显示action-sheet
  showActionSheet: function () {
      this.setData({ actionSheetHidden: false })
  },
  //点击取消
  actionSheetChange: function () {
    this.setData({ actionSheetHidden: true })
    
  },
  //条目点击事件
  actionSheetItem: function (event) {
     
      var showTel = this.data.company.mobile;
      console.log(showTel)

      wx.makePhoneCall({
        phoneNumber: showTel
      })
  },
  //取消电话咨询
  actionCancel:function(e){
    this.setData({ actionSheetHidden: true })
  },
  modalinput: function () {

    //检查是否已经登录，没登录就提示、跳转
    wx.getUserInfo({
      success:function(){

      },
      fail:function(res){
        //弹窗提示授权
        wx.showToast({
          title: '请先登录再预约',
          icon: 'error',
          duration: 1500
        });


        setTimeout(function(){
          wx.switchTab({
            url: '/pages/my/my',
          })
        },1500)

        return;
      }
    })

      this.setData({
          hiddenmodalput: !this.data.hiddenmodalput
      }) 
  },    //预约到店模态框
  cancel: function () {
      // console.log("取消")
      
      this.setData({
          hiddenmodalput: true
      });
  },//取消按钮  
  confirm: function (e) {
    var that = this;

    var orderName = that.data.orderName;
    var orderTel = that.data.orderTel;

    if (orderName == '' || orderTel == ''){
      wx.showModal({
        title: '提示',
        content: '预约人姓名和电话不能为空'
      })
      return;
    }
    var sex = that.data.sexState;

    // 提交数据
    var jsonData = { orderName: orderName, orderTel: orderTel, sex: sex};
 
    //提交
    api.post({
      url: 'portal/car/orderLook',
      data: jsonData,
      success: data => {
        console.log(data);
        if(data.code){
          //默认值为空
          that.setData({
            formDefault: '',
            orderName: '',
            orderTel: '',
            sexState: 0,
            hiddenmodalput: true
          })

          wx.showToast({
            title: data.msg,
            icon: 'success',
            duration: 1000
          });
        }else{
          wx.showToast({
            title: data.msg,
            icon: 'error',
            duration: 1000
          });
        }
        
      },
      fail: err => {
        wx.showToast({
          title: data.msg,
          icon: 'error',
          duration: 1000
        });
      }
    });

      console.log(e);

     
  }, //确认按钮
  sexClick:function(){
    var sexState = this.data.sexState
      this.setData({
        sexState: (sexState == 1)?0:1
      })
  },
  /**
   * 获取车详情
   */
  getCarDetail:function(){
    // console.log('getCarDetail')

    var that = this;
    var car_id = that.data.car_id;
    var ver_id = that.data.ver_id;
    
    if (ver_id == undefined || ver_id == '' || !ver_id) {
      ver_id = 0;
    }

    api.post({
      url: 'portal/car/detail',
      data: {
        car_id: car_id,
        ver_id: ver_id,
      },
      success: data => {
        
        if (data.code) {
          WxParse.wxParse('post_content', 'html', data.data.post_content, this, 30);
          //加入缓存
          wx.setStorage({
            key: "car_detail_" + car_id + '_v_' + ver_id,
            data: data.data
          })

          var swipertotalNum = data.data.more.photos.length             //获取轮播的总数
          that.setData({
            detail: data.data,
            swipertotalNum: swipertotalNum
          })

        } else {
          wx.showToast({
            title: data.msg,
            icon: 'error',
            duration: 1000
          });
        }
      },
      fail: err => {
      }
    });
  },

  /**
   * 返回首页
   */
  backIndex:function(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  /**
   * 点击显示地图
   */
  showMap: function () {
    var that = this;
    //获取经纬度
    var lng = Number(that.data.company.lng);
    var lat = Number(that.data.company.lat);

    wx.openLocation({
      latitude: lat,
      longitude: lng,
      scale: 28
    })
  },
  /**
  * 解析富文本内容
  */
  article2html: function (article, name) {
    var that = this;
    //解析富文本
    WxParse.wxParse(name, 'html', article, that, 5);
  },
  /**
   * 预约到店
   */
  orderTelChange:function(e){ 
    var orderTel = e.detail.value;
    this.setData({
      orderTel: orderTel
    })
  },
  orderNameChange: function (e) {
    var orderName = e.detail.value;
    this.setData({
      orderName: orderName
    })
  },
  /**
   * 跳转计算器
   */
  toCal:function(e){
    console.log(e)
    var form_id = e.detail.formId;
    //保存form_id
    app.cacheFormIDs(form_id);

    var url = e.target.dataset.url;

    wx.navigateTo({
      url: url,
    })
  }
})